package me.liuhui.mall.manager.service.impl;

import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import me.liuhui.mall.manager.service.AuthService;
import me.liuhui.mall.manager.service.PermissionService;
import me.liuhui.mall.manager.service.security.CurrentAdmin;
import me.liuhui.mall.manager.service.vo.auth.AuthVO;
import me.liuhui.mall.manager.utils.HttpUtil;
import me.liuhui.mall.repository.mapper.AdminLoginRecordMapper;
import me.liuhui.mall.repository.mapper.AdminMapper;
import me.liuhui.mall.repository.model.Admin;
import me.liuhui.mall.repository.model.AdminLoginRecord;
import me.liuhui.mall.repository.model.Permission;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created on 2020/10/23 11:23
 * <p>
 * Description: [TODO]
 * <p>
 * Company: []
 *
 * @author [清远]
 */
@Service
public class AuthServiceImpl implements AuthService {

    @Resource
    private AdminMapper adminMapper;
    @Resource
    private PermissionService permissionService;
    @Resource
    private AdminLoginRecordMapper adminLoginRecordMapper;

    @Override
    public CurrentAdmin login(String username) {
        Admin admin = adminMapper.selectByUsername(username);
        if (admin == null) {
            throw new UsernameNotFoundException("用户名或密码错误");
        }

        admin.setLastLoginTime(new Date());
        adminMapper.updateById(admin);
        AuthVO vo = new AuthVO();
        vo.setAdminId(admin.getId());
        vo.setRealName(admin.getRealName());
        vo.setUsername(admin.getUsername());

        AdminLoginRecord loginRecord = obtainRequest();
        loginRecord.setAdminId(admin.getId());
        loginRecord.setAdminUserName(admin.getUsername());
        adminLoginRecordMapper.insert(loginRecord);

        List<Permission> permissions = permissionService.currentPermissions(admin.getId());
        CurrentAdmin userDetails = new CurrentAdmin(admin);
        userDetails.setPermissions(permissions);
        return userDetails;
    }

    private AdminLoginRecord obtainRequest() {
        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        String agent = request.getHeader("User-Agent");
        UserAgent userAgent = UserAgentUtil.parse(agent);
        AdminLoginRecord loginRecord = new AdminLoginRecord();
        loginRecord.setCreateTime(new Date());
        loginRecord.setIp(HttpUtil.getRemoteAddr(request));
        loginRecord.setBrowser(userAgent.getBrowser().getName());
        loginRecord.setDevice(userAgent.getPlatform().getName());
        loginRecord.setOs(userAgent.getOs().getName());
        return loginRecord;
    }
}
